package com.example.manage_c20086.api;
import com.example.manage_c20086.cutil.cTool;
import com.pojo.Entity.Equipment;
import com.pojo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/api/eqm")
public class EquipmentApi {
    @Autowired
    private RestTemplate restTemplate;
    private static final String REST_URL_PREFIX="http://EQUIPMENT-MANAGEMENT/eqm";
    private static final String IDENTIFYING="identifying";
    @PostMapping(value = "rental")
    public Result<?> rental(Integer eID, Integer eInventory, HttpServletRequest httpServletRequest) throws InterruptedException {
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("eID",eID);
        map.add("eInventory",eInventory);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/rental",request,Result.class);
        return  result;
    }
    @PostMapping(value = "prerental")
    public Result<?> prerental(Integer eID, Integer eInventory,String vpretime,String vpreendtime,HttpServletRequest httpServletRequest) throws InterruptedException {
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("eID",eID);
        map.add("eInventory",eInventory);
        map.add("vPretime",vpretime);
        map.add("vPreEndtime",vpreendtime);
       System.out.println(eID+ " "+vpretime+" "+vpreendtime);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/prerental",request,Result.class);
        return  result;
    }
    @PostMapping("addeqm")
    public Result<?>addeqm(Equipment equipment, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        cTool.jsonToMap(equipment,map);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/addeqm",request,Result.class);
        return  result;
    }
    @PostMapping("upeqm")
    public Result<?>upeqm(Equipment equipment, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        cTool.jsonToMap(equipment,map);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/upeqm",request,Result.class);
        return  result;
    }
    @GetMapping("payeqm")
    public Result<?>payeqm(Integer erID,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("erID",erID);
        map.add("erType",1);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/payeqm",request,Result.class);
        return  result;
    }
    @GetMapping("nopayeqm")
    public Result<?>nopayeqm(Integer erID,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("erID",erID);
        map.add("erType",2);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/payeqm",request,Result.class);
        return  result;
    }
    @GetMapping("isreturn")
    public Result<?>isreturn(Integer erID, Integer erReturn, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("erID",erID);
        map.add("erReturn",1);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result= restTemplate.postForObject(REST_URL_PREFIX+"/isreturn",request,Result.class);;
        return result;
    }
    @GetMapping("getlpayrt")
    public Result<?>getlpayrt(Integer page, Integer number,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("page",page);
        map.add("number",number);
        map.add("erTYpe",1);
        map.add("erReturn",0);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/getlpayrt",request,Result.class);
        return  result;
    }
    @GetMapping("getlnopayrt")
    public Result<?>getlnopayrt(Integer page, Integer number,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("page",page);
        map.add("number",number);
        map.add("erTYpe",0);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/getlpayrt",request,Result.class);
        return  result;
    }
    @GetMapping("getlall")
    public Result<?>getlall(Integer page, Integer number,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("page",page);
        map.add("number",number);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/getlpayrt",request,Result.class);
        return  result;
    }
    @GetMapping("getlpay")
    public Result<?>getlpay(Integer page, Integer number,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("page",page);
        map.add("number",number);
        System.out.println(page+" "+number);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/getlrpay",request,Result.class);
        return  result;
    }
    @GetMapping("getleqm")
    public Result<?>getleqm(Integer page,Integer number,String key,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("page",page);
        map.add("number",number);
        map.add("key",key);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result= restTemplate.postForObject(REST_URL_PREFIX+"/getleqm",request,Result.class);
        return result;
    }
    @GetMapping ("geteqm")
    public Result<?>geteqm(Integer eID,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("eID",eID);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/geteqm",request,Result.class);
        return  result;
    }
    @GetMapping("deleteeqm")
    public Result<?>deleteeqm(Integer eID,HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("eID",eID);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result result=restTemplate.postForObject(REST_URL_PREFIX+"/deleteeqm",request,Result.class);
        return  result;
    }
}
